package com.css.fxfzmh.zcpt.sys.controller;

import com.css.fxfzmh.zcpt.sys.entity.DeptEntity;
import com.css.fxfzmh.zcpt.sys.service.SysDeptService;
import com.css.fxfzmh.base.response.RestResponse;
import com.css.fxfzmh.util.CreateGson;
import com.google.gson.Gson;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author Dbj
 * @date 2022年08月09日  15:17
 */
@RestController
@RequestMapping("/fxfz/sdept")
@Slf4j
public class SysDeptController {

    @Autowired
    private SysDeptService sysdeptService;
    /**
     * 根据用户id获取用户所属的主部门信息
     */
    @GetMapping("/getMainOrgByUserId")
    public RestResponse getMainOrgByUserId(@RequestParam("userId") String userId) {
        RestResponse restResponse = null;
        if(org.apache.commons.lang3.StringUtils.isBlank(userId)){
            restResponse = RestResponse.fail("缺失用户id!");
            return restResponse;
        }
        try{
            String jsonStr = sysdeptService.getMainDeptByUserId(userId);
            Gson gson = CreateGson.createGson();
            DeptEntity deptEntity = gson.fromJson(jsonStr,DeptEntity.class);
            restResponse = RestResponse.succeed(deptEntity);
        }catch (Exception e){
            log.error("查询机构失败!",e);
            restResponse = RestResponse.fail("查询机构失败!");
        }
        return restResponse;
    }
}
